﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using WikiDoc.Formatter;
using WikiDoc.Lines;

namespace WikiDoc.Parser
{
    /// <summary>
    /// 处理表格
    /// </summary>
    public class TableParser
    {
        /// <summary>
        /// Processes the specified argv.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <param name="formatter">The formatter.</param>
        public void Process(WikiReader reader, IFormatter formatter)
        {
            var keepgo = true;
            var separator = new[] {"||"};

            formatter.BeginTag("table");

            while (keepgo)
            {
                switch (reader.CurrentLine.Type)
                {
                    case LineTypes.Table:
                        var items = ((TableLine) reader.CurrentLine).Content.Split(separator, StringSplitOptions.None);

                        formatter.BeginTag("tr");
                        
                        foreach (var item in items)
                        {
                            formatter.BeginTag("td");
                            formatter.AppendContent(WikiTranslater.SimpleTranslate(item));
                            formatter.EndTag("td");
                        }
                        
                        formatter.EndTag("tr");
                        reader.ReadLine();
                        break;

                    default:
                        keepgo = false;
                        break;
                }
            }

            formatter.EndTag("table");
        }
    }
}
